package cn.david.order.controller;

import cn.david.basic.util.AjaxResult;
import cn.david.basic.util.LoginContext;
import cn.david.basic.util.PageList;
import cn.david.order.domain.AdoptOrder;
import cn.david.order.query.AdoptOrderQuery;
import cn.david.order.service.IAdoptOrderService;
import cn.david.user.domian.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/adoptOrder")
public class AdoptOrderController {
    @Autowired
    private IAdoptOrderService adoptOrderService;
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String,Object> params, HttpServletRequest request){
        try {

            String pay = adoptOrderService.submit(params,LoginContext.getLoginInfo(request));
            return AjaxResult.me().setResultObj(pay);
        } catch (Exception e) {

            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下单失败"+e.getMessage());
        }

    }
    //平台管理人员
    @PostMapping("/admin")
    public PageList<AdoptOrder> admin(@RequestBody AdoptOrderQuery query,HttpServletRequest request){
        Logininfo loginInfo = LoginContext.getLoginInfo(request);
        return adoptOrderService.admin(query,loginInfo.getId());
    }
    //前台用户的
    @PostMapping("/user")
    public PageList<AdoptOrder> user(@RequestBody AdoptOrderQuery query,HttpServletRequest request){
        Logininfo loginInfo = LoginContext.getLoginInfo(request);
        return adoptOrderService.user(query,loginInfo.getId());
    }
    @GetMapping("/{id}")
    public AdoptOrder info(@PathVariable("id") Long id){
        AdoptOrder one = adoptOrderService.getById(id);
        return one;
    }

}
